package com.zhen.strategy.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.zhen.strategy.menu.BusiNewEnum;
import com.zhen.strategy.service.BusiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

@Service
@Slf4j
public class BusiServiceImpl implements BusiService {
    @Autowired
    ApplicationContext context;

    @Override
    public String call(String para, String type) {
        BusiNewEnum busiNewEnum = BusiNewEnum.getEnumByBusiType(type);
        String result = "";
        try {
            String serviceName = busiNewEnum.getInvoke().getService();
            Object bean = context.getBean(serviceName);
            Method method = bean.getClass().getMethod(busiNewEnum.getInvoke().getMethod(), String.class);
            Object invokeResult = method.invoke(bean, para);
            JSONObject jsonObject = JSONObject.parseObject(invokeResult.toString());
            result = jsonObject.getString("key");
        } catch (Exception e) {
            log.info("BusiService.call方法执行异常，{}", e);
        }

        return result;
    }
}
